using System;
using System.Globalization;
using System.Reflection.Emit;

namespace RaisingStudio.SmallBasicCompiler.Statements
{
    public class LabelStatement : Statement
    {
        public TokenInfo LabelToken;
        public TokenInfo ColonToken;
        public override void AddSymbols(SymbolTable symbolTable)
        {
            if (this.LabelToken.Token != Token.Illegal)
            {
                symbolTable.AddLabelDefinition(this.LabelToken);
            }
        }
        public override void PrepareForEmit(CodeGenScope scope)
        {
            Label value = scope.ILGenerator.DefineLabel();
            scope.Labels.Add(this.LabelToken.NormalizedText, value);
        }
        public override void EmitIL(CodeGenScope scope)
        {
            Label loc = scope.Labels[this.LabelToken.NormalizedText];
            scope.ILGenerator.MarkLabel(loc);
        }
        public override string ToString()
        {
            return string.Format(CultureInfo.CurrentUICulture, "{0}{1}\r\n", new object[]
			{
				this.LabelToken.Text, 
				this.ColonToken.Text
			});
        }
    }
}
